package com.zlx.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlx.dao.BaseDao;
import com.zlx.entities.Menu;
import com.zlx.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author 周罗校
 * @Description 菜单的控制层
 * @ClassName MenuServlet
 * @date 2020.09.22 17:55:40
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@WebServlet("/menu/*")
public class MenuServlet extends BaseServlet{
    private MenuService service = new MenuService();


    protected void getMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Menu> allMenuInfo = service.getAllMenuInfo();
        //过滤并获取一级标题
        List<Menu> parentList = allMenuInfo.stream().filter((n -> {
            return n.getType().equals(1);
        })).collect(Collectors.toList());
        //过滤并获取二级标题
        List<Menu> sonList = allMenuInfo.stream().filter((n -> {
            return n.getType().equals(2);
        })).collect(Collectors.toList());

        //使用map集合返回数据
        Map<String, List<Menu>> map = new HashMap<>();
        map.put("parentList", parentList);
        map.put("sonList", sonList);
        //转换成Json字符串
        String s = JSONObject.toJSONString(map);
        //写回去Json串给Ajax
        response.getWriter().write(s);
    }
}
